<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ page import="java.sql.*" %>
<%@ page import="java.net.URLEncoder" %>
<%@ page import="java.nio.charset.StandardCharsets"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>将桌子状态设为空闲</title>
</head>
<body>
<%
String customer_name = request.getParameter("customer_name");
String restaurant_name = request.getParameter("restaurant_name");
String num = request.getParameter("table_num");

//连接数据库
try {
  Class.forName("org.mariadb.jdbc.Driver");
  Connection conn = DriverManager.getConnection("jdbc:mariadb://localhost:3306/mealdb?user=root&password=1230321");
  Statement stmt = conn.createStatement();

  String sql1 = "UPDATE `table` SET state = '空闲' WHERE num = '" + num + "'";
  stmt.executeUpdate(sql1);

  String sql2 = "UPDATE `customers` SET table_num = '" + num + "' WHERE `name` = '" + customer_name + "'";
  stmt.executeUpdate(sql2);

  // 对顾客名字进行编码
  String encodedCustomerName = URLEncoder.encode(customer_name, StandardCharsets.UTF_8);
  // 对餐厅名字进行编码
  String encodedRestaurantName = URLEncoder.encode(restaurant_name, StandardCharsets.UTF_8);

  // 构建编码后的URL
  String encodedUrl = "receptionistIndex.jsp?customer_name=" + encodedCustomerName + "&restaurant_name=" + encodedRestaurantName;

  // 重定向到餐厅首页
  response.sendRedirect(encodedUrl);

  // 关闭数据库连接
  stmt.close();
  conn.close();

} catch (ClassNotFoundException | SQLException e) {
  e.printStackTrace(); // 打印异常详情到控制台，用于调试
  // 可以在这里处理异常，例如重定向到错误页面
}

%>
</body>
</html>